package com.tengju.user.application.user;

import com.tengju.data.domain.shared.JsonUtil;
import com.tengju.support.application.robot.FeiShuRobotService;
import com.tengju.support.domain.model.robot.*;
import com.tengju.user.domain.model.department.DepartmentTree;
import com.tengju.user.domain.model.sentiment.Sentiment;
import com.tengju.user.domain.model.sentiment.SentimentRepository;
import com.tengju.user.domain.model.user.SsoUserId;
import com.tengju.user.domain.model.user.StaffSimpleInfo;
import com.tengju.user.domain.service.DepartmentExternalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: Dylan
 * @date: 2021/8/25 5:09 下午
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserSentimentRemindJobService {


    private final SentimentRepository sentimentRepository;
    private final DepartmentExternalService departmentExternalService;
    private final FeiShuRobotService feiShuRobotService;

    @Value("${user.sentiment.remind}")
    private String remindConfiguration;

    public void execute() {
        LocalDateTime startDayMin = LocalDateTime.now();
        LocalDateTime nextDayMin = startDayMin.plusDays(1L);
        // 获取departmentIds
        List<DepartmentTree> departmentTree = departmentExternalService.getDepartmentTree();
        List<Long> departmentIdList = departmentTree.stream().map(v -> v.getValue().getValue()).collect(Collectors.toList());
        List<StaffSimpleInfo> staffSimpleInfos = departmentExternalService.getStaffSimpleInfosByDepartmentIds(departmentIdList);
        List<SsoUserId> ssoUserIds = staffSimpleInfos.stream().map(StaffSimpleInfo::getSsoUserId).distinct().filter(Objects::nonNull).collect(Collectors.toList());
        List<Sentiment> sentimentList = sentimentRepository.getBySsoUserIds(startDayMin, nextDayMin, ssoUserIds);

        Map<Long, Long> ssoSentiment = sentimentList.stream()
                .collect(Collectors.toMap(s -> s.getSsoUserId().getValue(), s -> s.getSsoUserId().getValue(), (s1, s2) -> s1));

        Map<String, String> remindMap = JsonUtil.toMap(remindConfiguration);
        if (CollectionUtils.isEmpty(remindMap)) {
            return;
        }
        if (Boolean.parseBoolean(remindMap.get("switch"))) {
            for (SsoUserId ssoUserId : ssoUserIds) {
                try {
                    if (!ssoSentiment.containsKey(ssoUserId.getValue())) {
                        FeiShuRobotSendImMsgPostContent postContent = new FeiShuRobotSendImMsgPostContent();
                        FeiShuRobotSendImMsgPostContentContent postContentContent = new FeiShuRobotSendImMsgPostContentContent();
                        List<List<FeiShuRobotSendImMsgPostContentContentTemplate>> templateList = new ArrayList<>();
                        List<FeiShuRobotSendImMsgPostContentContentTemplate> templateList1 = new ArrayList<>();
                        FeiShuRobotSendImMsgPostContentContentTemplateText text = new FeiShuRobotSendImMsgPostContentContentTemplateText();
                        text.setText(remindMap.get("text"));
                        templateList1.add(text);
                        FeiShuRobotSendImMsgPostContentContentTemplateA a = new FeiShuRobotSendImMsgPostContentContentTemplateA();
                        a.setText("点击前往");
                        a.setHref(remindMap.get("url"));
                        templateList1.add(a);
                        templateList.add(templateList1);
                        postContentContent.setContent(templateList);
                        postContent.setZh_cn(postContentContent);
                        feiShuRobotService.sendMsg(ssoUserId, FeiShuRobotSendMsgType.POST, postContent);
                    }
                } catch (Exception e) {
                    log.error("sendImMsg error", e);
                }
            }
        }
    }
}
